<?php

declare(strict_types=1);

namespace App\Listener;

use Hyperf\Event\Annotation\Listener;
use Psr\Container\ContainerInterface;
use Hyperf\Event\Contract\ListenerInterface;
use App\Event\DoLogEvent;
use App\Model\admin\LogModel;
use Hyperf\HttpServer\Router\Dispatched;


class DoLogListener implements ListenerInterface
{


    public function listen(): array
    {
        return [
            DoLogEvent::class,
        ];
    }

    public function process(object $event)
    {
		$action = request()->getAttribute(Dispatched::class)->handler->callback;		

		if(in_array($action[1],['add','update','delete'])){
			$content = request()->all();
			if ($content) {
				foreach ($content as $k => $v) {
					if (is_string($v) && strlen($v) > 200 || stripos($k, 'password') !== false) {
						unset($content[$k]);
					}
				}
			}
		
			$data['application_name'] = $event->data['application_name'];
			$data['username'] = $event->data['username'];
			$data['url'] = request()->url();
			$data['ip'] = reaip();
			$data['useragent'] = request()->header('user-agent');
			$data['content'] = json_encode($content,JSON_UNESCAPED_UNICODE);
			$data['create_time'] = time();
			$data['type'] = 2;
			
			LogModel::insert($data);
		}
    }
}
